package topo.service;

import com.zhtelecom.common.topograph.TopoDataSource;
import com.zhtelecom.common.topograph.TopoLink;
import com.zhtelecom.common.topograph.TopoNode;
import java.util.Vector;
import topo.MainFrame;
import topo.data.NodeDevice;
import topo.data.NodeLink;
import topo.data.NodeType;

public class TopoDataManager {
    private static TopoDataManager topoDataManager;

    public static TopoDataManager getInstance() {
        if (topoDataManager == null) {
            topoDataManager = new TopoDataManager();
        }
        return topoDataManager;
    }

    public void delNode(NodeDevice node) {
        MainFrame.source.removeTopoDataByUserID(node);
    }

    public void updateNode(NodeDevice node) {
        TopoNode topoNode = (TopoNode) MainFrame.source.getTopoObject(node);
        NodeType type = NodeType.getNodeTypeByKey(node.getNodeType());
        topoNode.setDisplayName(node.getIpAddress());
        if (type != null) {
            topoNode.setImageIcon(type.getTypeImage());
        } else {
            topoNode.setImageIcon(TopoNode.defaultIcon);
        }
        topoNode.fireChanged();
    }

    public void addLink(NodeLink linkDB) {
        NodeDevice localNode = getNodeByNodeName(linkDB.getFromNode());
        NodeDevice remoteNode = getNodeByNodeName(linkDB.getToNode());
        TopoNode topoNodeSrc = (TopoNode) MainFrame.source.getTopoObject(localNode);
        TopoNode topoNodeTaget = (TopoNode) MainFrame.source.getTopoObject(remoteNode);

        if ((localNode != null) && (remoteNode != null)) {
            TopoLink linkView = new TopoLink(linkDB, topoNodeSrc, topoNodeTaget);
            linkView.setShowArrow(false);
            MainFrame.source.addTopoData(linkView);
        }
    }

    public void delLink(NodeLink link) {
        MainFrame.source.removeTopoDataByUserID(link);
    }

    public void updateLink(NodeLink link) {
        TopoLink topoLink = (TopoLink) MainFrame.source.getTopoObject(link);
        topoLink.fireChanged();
    }

    public void addNode(NodeDevice neDB) {
        try {
            TopoNode topoNode = new TopoNode(neDB);

            topoNode.setDisplayName(neDB.getIpAddress());
            topoNode.setXY(neDB.getPoseX(), neDB.getPoseY());

            NodeType type = NodeType.getNodeTypeByKey(neDB.getNodeType());
            if (type != null) {
                topoNode.setImageIcon(type.getTypeImage());
            } else {
                topoNode.setImageIcon(TopoNode.defaultIcon);
            }
            MainFrame.source.addTopoData(topoNode);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public TopoNode getTopoNodeByName(String nodeName) {
        NodeDevice node = new NodeDevice();
        node.setNodeName(nodeName);
        return (TopoNode) MainFrame.source.getTopoObject(node);
    }

    public NodeDevice getNodeByNodeName(String nodeName) {
        TopoNode topoNode = getTopoNodeByName(nodeName);
        if (topoNode != null) {
            return (NodeDevice) topoNode.getUserID();
        }

        return null;
    }

    public boolean checkExistDeviceByIP(String ip) {
        Vector vector = MainFrame.source.getAllTopoNode();
        NodeDevice nodeDevice = new NodeDevice();
        for (int i = 0; i < vector.size(); i++) {
            nodeDevice = (NodeDevice) ((TopoNode) vector.get(i)).getUserID();
            if (nodeDevice.getIpAddress().equals(ip)) {
                return true;
            }
        }
        return false;
    }
}